package cn.com.winning.ecare.common;

import java.text.SimpleDateFormat;
import java.util.Calendar;

import cn.com.winning.ecare.activity.R;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class CalendarDialog extends Dialog {
	private Context mContext;
	private TextView mTopdate;
	private ViewPager mViewPager;// 日期切换页面
	private RelativeLayout preButton; // 切换至前一个月
	private RelativeLayout nextButton; // 切换至下一个月
	private int itemCount = 120;// 最多可以显示120个月

	public CalendarDialog(Context context, boolean cancelable,
			OnCancelListener cancelListener) {
		super(context, cancelable, cancelListener);
		// TODO Auto-generated constructor stub
		mContext = context;
	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Log.i("aaa", "CalendarDialog onCreate");
		this.setContentView(R.layout.calendar_main);

		findView();

		setListener();

		mTopdate.postDelayed(new Runnable() {

			public void run() {
				// 加载日历
				initViewPager();

			}
		}, 100);

	}

	private void findView() {
		// 日历设置
		mTopdate = (TextView) findViewById(R.id.top_date);
		String month = monthFormat(Calendar.getInstance());
		mTopdate.setText(month);

		findViewById(R.id.weekTitle).setBackgroundColor(
				this.getContext().getResources()
						.getColor(R.color.backgroundMYD));

		preButton = (RelativeLayout) this.findViewById(R.id.btn_pre_month);
		nextButton = (RelativeLayout) this.findViewById(R.id.btn_next_month);
	}

	private void setListener() {
		preButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				int currentItem = mViewPager.getCurrentItem();
				mViewPager.setCurrentItem(currentItem - 1);
			}
		});
		nextButton.setOnClickListener(new View.OnClickListener() {

			public void onClick(View v) {
				// TODO Auto-generated method stub
				int currentItem = mViewPager.getCurrentItem();
				mViewPager.setCurrentItem(currentItem + 1);
			}
		});
	}

	private void initViewPager() {
		mViewPager = (ViewPager) findViewById(R.id.ViewPager1);
		mViewPager.setAdapter(new ImageAdapter());
		mViewPager.setCurrentItem(itemCount / 2);
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {

			public void onPageSelected(int position) {
				Calendar calendar = Calendar.getInstance();
				int num = position - itemCount / 2;
				calendar.add(Calendar.MONTH, num);
				String monthStr = monthFormat(calendar);
				mTopdate.setText(monthStr);

				mViewPager.getAdapter().notifyDataSetChanged();

			}

			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			public void onPageScrollStateChanged(int arg0) {
			}
		});

	}

	private String monthFormat(Calendar calendar) {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
		String format = dateFormat.format(calendar.getTime());
		String[] strs = format.split("-");
		String monthStr = strs[0] + "年" + strs[1] + "月";
		return monthStr;
	}

	public CalendarDialog(Context context, int theme) {
		super(context, theme);
		// TODO Auto-generated constructor stub
		mContext = context;
	}

	public CalendarDialog(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext = context;
	}

	private class ImageAdapter extends PagerAdapter {

		@Override
		public int getCount() {

			return itemCount;
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {

			return arg0 == arg1;
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			Log.i("aaa", "instantiateItem: " + position);

			View view = LayoutInflater.from(mContext).inflate(
					R.layout.sslist_item, null);
			ListView listView = (ListView) view.findViewById(R.id.listView1);
			int num = position - itemCount / 2;
			ListAdapter adapter = new DateAdapter(CalendarDialog.this, num,
					listView);
			listView.setAdapter(adapter);
			view.setTag(position);
			container.addView(view);
			return view;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			Log.i("aaa", "destroyItem: " + position);
			container.removeView(container.findViewWithTag(position));
		}

		@Override
		public int getItemPosition(Object object) {
			// TODO Auto-generated method stub
			// return super.getItemPosition(object);
			return POSITION_NONE;
		}

	}

}
